﻿<?php
   require_once("../class/mysql_help.class.php");
   require_once("../config/config.php");
   class Company
   {
      var $id,$CName,$pasword,$intro,$companyName,$sign;
      var $dbHelper;
      
      public function __construct()
      {
      	$this->dbHelper=new db_Mysql();
      }
      //用户登录
      public function login($CName,$password) 
      {
      	global $pre_md5;
      	$this->dbHelper->dbconnect();
      	$cmd=" select * from company where name='".$CName."'";
      	$result=$this->dbHelper->execute($cmd);    
      	$row=$this->dbHelper->fetch_array($result);
      	$this->dbHelper->free_result($result);
      	$is=is_array($row);
      	$this->dbHelper->dbclose();
      	$ss=$is?(md5($password.$pre_md5)==$row["password"]):false;
      	if($ss)
      	{
      		return $row["id"];//返回公司的id
      	}
		else return 0; 	
      }
      // 增加用户
      public function UserAdd($CName,$password,$companyName)
      {
      	global $pre_md5;
      	$this->dbHelper->dbconnect();
      	$sign=false;
      	$cmd=" insert into company(name,password,companyName,sign) values('".$CName."','".md5($password.$pre_md5)."','".$companyName."','".$sign."')";
      	$result=$this->dbHelper->insert($cmd);
      	$this->dbHelper->dbclose();
      	return $result;
      }
      //删除用户
      public function UserDelete($Id)
      {
      	$this->dbHelper->dbconnect();
      	$cmd="delete from company where id='".$Id."' ";
      	$result=$this->dbHelper->delete($cmd);
      	$this->dbHelper->dbclose();
      	return $result;
      }
      //更新用户
      public function UserUpdate($id,$name,$companyName,$intro)
      {
      	$this->dbHelper->dbconnect();
      	$cmd="update company set name='".$name."',CompanyName='".$companyName."',intro='".$intro."' where id='".$id."'";
      	$result=$this->dbHelper->update($cmd);
      	$this->dbHelper->dbclose();
      	return $result;
      }
      //修改密码
      public function UpdatePassword($id,$password)
      {
      	$this->dbHelper->dbconnect();
      	$cmd="update company set password='".$password."' where id='".$id."' ";
      	$result=$this->dbHelper->update($cmd);
      	$this->dbHelper->dbclose();
      	return $result;
      }
      
      //判断用户是否已经存在
      public function IsNameExist($CName)
      {
      	$this->dbHelper->dbconnect();
      	$cmd="select * from company where name='".$CName."' ";
      	$result=$this->dbHelper->get_rows($cmd);
      	$this->dbHelper->dbclose();
      	if($result>0)
      	{
      		return true;
      	}
      	else return false;
      }
      //设置企业通过认证
      public function setlimit($id)
      {
      	$this->dbHelper->dbconnect();
      	$sign=true;
      	$cmd="update company set limit='".$sign."' where id='".$id."' ";
      	$result=$this->dbHelper->update($cmd);
      	$this->dbHelper->dbclose();
      	return true;
      }
      //设置企业未通过认证
      public function UnSetlimit($id)
      {
      	$this->dbHelper->dbconnect();
      	$sign=false;
      	$cmd=" update company set limit='".$sign."'where id='".$id."' ";
      	$result=$this->dbHelper->update($cmd);
      	$this->dbHelper->dbclose();
      	return true;
      }
      //功能：根据用户id返回一条用户的完整信息
      public function GetUserDataById($userId)
      {
      	$this->dbHelper->dbconnect();
		$cmd = "Select * From company Where id = '".$userId."'";
		$result = $this->dbHelper->execute($cmd);
		$row = $this->dbHelper->fetch_array($result);
		$this->id=$row["id"];
		$this->CName=$row["name"];
		$this->companyName=$row["companyName"];
		$this->sign=$row["sign"];
		$this->dbHelper->free_result($result);
	  }
	  
	  /*功能：根据企业id返回企业的名字
	  @ $id_company 企业的id
	  //返回：企业的名字
	  */
	  public function GetCompanyName($id_company)
	  {
		$cmd = "Select companyName From company Where id = '".$id_company."'";
		$this->dbHelper->dbconnect();
		$result = $this->dbHelper->execute($cmd);
		$this->dbHelper->dbclose();
		$row = $this->dbHelper->fetch_array($result);
		$this->dbHelper->free_result($result);
		return $row["companyName"];
	  }
	  
	  /*下面的不属于此类，仅用于测试*/
	  
	  /*功能：展示查询出来的留言记录
		@ $id_comment  需要展示的评论的id
		//返回：封装好的html的字符串
		*/
		/*public function ShowOneCommentById($id_comment)
		{
			$row = $this->SelectOneById($id_comment);
			if($row == null) return "";//若为空 则直接返回
			//echo '<script>alert("running!");</script>';
			$str = '<div class="message_m">
						<div class="message_m_logo"><img src="../images/12974102173752464b.gif" width="60" height="60" /></div>
						<div class="message_m_message">
							<span class="fc_cpn_names">'.$row["name"].'：</span>'.$row["content"].'
						</div>
						<div class="clear"></div>
					</div>
					<div class="message_re">评论时间 : '.date("Y-m-d",$row["date_public"]).' | 回复('.$row["num_reply"].')</div>
				';
			return $str;
		}
		
		/*功能：根据id查询一条留言记录（主要用来显示新增的评论）
		@ $id_comment  评论的id
		//返回：数组类型 $row;
		*/
	/*	public function SelectOneById($id_comment)
		{
			$cmd = "select * from message_board where id = '".$id_comment."'";
			$this->dbHelper->dbconnect();
			$result = $this->dbHelper->execute($cmd);
			$this->dbHelper->dbclose();
			$row = $this->dbHelper->fetch_array($result);
			$this->dbHelper->free_result($result);
			return $row;
		}
	  
		/*功能：添加留言
		@ $id_employMessage 招聘信息的id
		@ $id_role  留言人的id
		@ $type_role  留言人的角色(毕业生或者企业)
		@ $content  留言的内容
		*/
	/*	public function AddMessage($id_employMessage,$id_role,$type_role,$content)
		{
			$num_reply = 0;//新增留言回复数默认为0
			$date_public = time();
			//获取留言人的名字
			$cmd = null;
			if($type_role == 1)//毕业生
			{
				$cmd = "Select name From graduate Where id = '".$id_role."'";
			}
			else if($type_role == 2)//企业
			{
				$cmd = "Select companyName as name From company Where id = '".$id_role."'";
			}
			//查询数据库获取名字
			$this->dbHelper->dbconnect();
			$result = $this->dbHelper->execute($cmd);
			$row = $this->dbHelper->fetch_array($result);
			$this->dbHelper->free_result($result);
			$this->dbHelper->dbclose();
			$cmd = "Insert Into message_board(id_employ_message,id_role,name,type_role,content,num_reply,date_public) values ('".
				$id_employMessage."','".$id_role."','".$row["name"]."','".$type_role."','".$content."','".$num_reply."','".$date_public."')";
			$this->dbHelper->dbconnect();
			$id_added = $this->dbHelper->insert($cmd);//获取插入数据的id
			$this->dbHelper->dbclose();
			return $id_added;//返回新插入数据的id
		}*/
   }
  
?>